We can create our own custom data types like how we can create our own functions.
Let’s create a person data type to bind a name and number together!
typedef struct
{
string name;
string number;
}
person;Now we can re-implement the phonebook:
// Linear Search Through Names
person people[2];
person[0].name = "Carter";
person[0].number = "+1-617-495-1000";
person[1].name = "David";
person[1].number = "+1-949-468-2750";
for (int i = 0; i < 2; i++) {
if ((strcomp(people[i].name, "David") == 0) { // Found
printf("Found %s". people[i].number);
return 0;
}
}
return 1; // Not Foundstruct because it still relies on an honor system (less encapsulation).